package com.vito.demo.sb2.business.daos

import org.hibernate.SessionFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.orm.hibernate5.HibernateTemplate

/**
 * Created by lingzhiyuan.
 * Date : 16/3/31.
 * Time : 下午8:53.
 * Description:
 *
 */

open class BaseDao @Autowired constructor(val hibernateTemplate:
                                          HibernateTemplate,
                                          val sessionFactory:
                                          SessionFactory) {

    open fun <T> findAll(clazz: Class<T>): List<T> {
        var session = sessionFactory.currentSession
        var criteria = session.createCriteria(clazz)
        return criteria.list() as List<T>
    }

    open fun <T> getById(clazz: Class<T>, id: Long): T {
        val `object` = hibernateTemplate.get(clazz, id)
        return `object`

    }

    open fun saveOrUpdate(`object`: Any) {
        hibernateTemplate.saveOrUpdate(`object`)
    }

    open fun update(`object`: Any) {
        hibernateTemplate.update(`object`)
    }

    open fun batchDelete(list: List<Any>) {
        hibernateTemplate.deleteAll(list)
    }

    open fun delete(`object`: Any) {
        hibernateTemplate.delete(`object`)
    }


}